package com.boe.hbd.client.service.support;

import java.util.List;

import android.content.Context;

import com.boe.hbd.client.Constants;
import com.boe.hbd.client.domain.Comment;
import com.boe.hbd.client.domain.Landscape;
import com.boe.hbd.client.domain.MediaEntity;
import com.boe.hbd.client.persistence.EntityManager;
import com.boe.hbd.client.service.LandscapeService;
import com.boe.hbd.client.service.ServiceManager;
import com.boe.hbd.client.utils.ChainedMap;
import com.boe.hbd.client.utils.Utils;
import com.boe.hbd.client.utils.GpsPoint;

public class LandscapeServiceSupport implements LandscapeService {

	private boolean initialized = false;
	private EntityManager entityManager = null;

	public boolean isInitialized() {
		return initialized;
	}

	public void initialize(Context context) {
		initialized = true;
		entityManager = ServiceManager.getInstance(null).getService(EntityManager.class);
	}

	public void pause(Context context) {
    }

	public void resume(Context context) {
    }
	
	public void destroy(Context context) {
	}

	public List<MediaEntity> findPhotosByFilterOfLandscape(Long landscapeId,String token) {
		if (token != null && !token.equals("")) {
			token = "%" + token + "%";
		} else {
			token = "%%" ;
		}
		List<MediaEntity> entities = entityManager.query(MediaEntity.class, "mediasByFilterOfLandscape",
		        new ChainedMap<String, Object>().chainedPut("landscapeId", landscapeId).chainedPut("token", token).chainedPut("type", Constants.MEDIA_TYPE_IMAGE));
		return entities;
	}

	public List<Comment> findCommentsByFilterOfLandscape(Long landscapeId,String token) {
		if (token != null && !token.equals("")) {
			token = "%" + token + "%";
		} else {
			token = "%%" ;
		}
		List<Comment> entities = entityManager.query(Comment.class, "commentsByFilterOfLandscape",
		        new ChainedMap<String, Object>().chainedPut("landscapeId", landscapeId).chainedPut("token", token).chainedPut("type", Constants.MEDIA_TYPE_IMAGE));
		return entities;
	}
	
	public List<MediaEntity> findVoicesByFilterOfLandscape(Long landscapeId,String token) {
		if (token != null && !token.equals("")) {
			token = "%" + token + "%";
		} else {
			token = "%%" ;
		}
		List<MediaEntity> entities = entityManager.query(MediaEntity.class, "mediasByFilterOfLandscape",
		        new ChainedMap<String, Object>().chainedPut("landscapeId", landscapeId).chainedPut("token", token).chainedPut("type", Constants.MEDIA_TYPE_VOICE));
		return entities;
    }
	
	public List<Landscape> findLandscapesByArea(GpsPoint topLeft, GpsPoint bottomRight, GpsPoint here,String name , Long startTime , Long endTime ){
		double latitude1   = topLeft.getLatitude();
		double latitude2   = bottomRight.getLatitude();
		double longtitude1 = topLeft.getLongitude();
		double longtitude2 = bottomRight.getLongitude();
		double temp = 0.0d;
		
		if (latitude1 > latitude2) {
			temp = latitude2;
			latitude2 = latitude1;
			latitude1 = temp;
		}
		if (longtitude1 > longtitude2) {
			temp = longtitude2;
			longtitude2 = longtitude1;
			longtitude1 = temp;
		}
		
		if (name == null) {
			name = "%%";
		} else {
			name = "%" + name + "%" ;
		}
		
		if (startTime == null) {
			startTime = 0L ;
		}
		
		if (endTime == null) {
			endTime = Long.MAX_VALUE;
		}
		
		List<Landscape> landscapes = entityManager.query(Landscape.class, "landscapeByArea", new ChainedMap<String, Object>()
				.chainedPut("latitude1", latitude1)
				.chainedPut("latitude2", latitude2)
				.chainedPut("longtitude1", longtitude1)
				.chainedPut("longtitude2", longtitude2)
				.chainedPut("name", name)
				.chainedPut("startTime", startTime)
				.chainedPut("endTime", endTime));
		if (here != null) {
			for (Landscape landscape : landscapes) {
				Double dis = Utils.distance(landscape.getLatitude(), landscape.getLongtitude(), here.getLatitude(), here.getLongitude());
				if (dis != null) {
				    landscape.setName(landscape.getName() + "["+ dis.intValue() +"]");
				}
			}
		}

		return landscapes;
	}

}
